<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd('0',1)">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type===1" type="text" @click="hAdd(row.id,2)">添加</el-button>
              <el-button type="text" @click="hEdit(row)">编辑</el-button>
              <el-button type="text" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!--  -->
      <el-dialog :visible.sync="showDialog" :title="isEdit?'新增':'修改'" @close="resetForm">
        <!-- 表单内容 -->
        <el-form ref="addForm" label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="hSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, addPermission, updatePermission, delPermission } from '@/api/permissions'
import { tranDateToTreeDate } from '@/utils/index'
export default {
  data() {
    return {
      isEdit: false,
      showDialog: false,
      list: [], // 用于保存获取的权限列表
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: 0,
        pid: '',
        type: 1
      }
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    // 提交
    hSubmit() {
      this.isEdit ? this.AddPermission() : this.EditPermission()
    },
    // 删除权限
    hDel(id) {
      this.$confirm('你确定要删除吗?').then(async() => {
        const res = await delPermission(id)
        this.$message.success(res.message)
        this.loadPermissionList()
      }).catch(err => console.log(err))
    },
    // 添加权限信息
    hAdd(pid, type) {
      this.isEdit = true
      // 由于添加按钮级别的是需要pid=父的pid 且type 要等于2
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    // 修改权限信息
    hEdit(data) {
      this.isEdit = false
      this.showDialog = true
      // 因为前面已经获取过数据了 所以可以用row直接拿到获取的数据
      this.formData = { ...data }
    },
    // 更新权限
    async EditPermission() {
      const res = await updatePermission(this.formData)
      console.log(res)
      this.showDialog = false
      this.loadPermissionList()
    },
    // 添加权限信息
    async AddPermission() {
      try {
        const res = await addPermission(this.formData)
        console.log(res)
        this.$message.success(res.message)
        this.loadPermissionList()
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    // 重置表单
    resetForm() {
      this.$refs.addForm.resetFields()
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: 0,
        pid: '',
        type: 1
      }
    },
    // 获取权限列表
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList', res)
        // 保存数据
        this.list = tranDateToTreeDate(res.data)
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }
}
</script>
